<!--
 * @Descripttion: HN-SaaS管理系统
 * @version: 1.0.0
 * @Author: 侯建军
 * @Date: 2020-09-04 10:56:21
 * @LastEditors: 侯建军
 * @LastEditTime: 2020-09-12 11:32:12
-->
<template>
  <div>
    <!-- <el-container>
      <el-header class="keyword">
        <el-form :inline="true" ref="form" :model="keyword" label-width="80px">
          <el-form-item>
            <el-button type="primary" @click="addUser">添加用户</el-button>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input placeholder="按姓名查询" v-model="keyword.username"></el-input>
          </el-form-item>
          <el-form-item label="科室">
            <el-select v-model="keyword.department" placeholder="请选择">
              <el-option
                v-for="item in departments"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="级别">
            <el-select v-model="keyword.level" placeholder="请选择">
              <el-option
                v-for="item in levels"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loadUsers(true)">查询</el-button>
          </el-form-item>
        </el-form>
      </el-header>
      <el-main>
        <el-table
          ref="multipleTable"
          :data="userData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="账号" width="120" prop="username"></el-table-column>
          <el-table-column prop="realName" label="姓名" width="120"></el-table-column>
          <el-table-column label="角色" show-overflow-tooltip>
            <template slot-scope="scope">
              <span
                v-for="role in roleData"
                :key="role.id"
              >{{role.id==scope.row.roleId?role.roleName:""}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="department.name" label="科室" show-overflow-tooltip></el-table-column>
          <el-table-column prop="job.name" label="职称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="level.name" label="等级" show-overflow-tooltip></el-table-column>
          <el-table-column prop="createDate" label="创建日期" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="keyword.currentPage"
          :page-size="keyword.pageSize"
          class="page"
          small
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </el-main>
    </el-container>-->
    <el-container>
      <el-header class="keyword">
        <el-form :inline="true" ref="form" :model="keyword" label-width="80px">
          <el-form-item>
            <el-button type="primary" @click="addUser">添加用户</el-button>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input placeholder="按姓名查询" v-model="keyword.username"></el-input>
          </el-form-item>
          <el-form-item label="科室">
            <el-select v-model="keyword.department" placeholder="请选择">
              <el-option
                v-for="item in departments"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="级别">
            <el-select v-model="keyword.level" placeholder="请选择">
              <el-option
                v-for="item in levels"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="loadUsers(true)">查询</el-button>
          </el-form-item>
        </el-form>
      </el-header>
      <el-main>
        <el-table
          ref="multipleTable"
          :data="userData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="账号" width="120" prop="username"></el-table-column>
          <el-table-column prop="realName" label="姓名" width="120"></el-table-column>
          <el-table-column label="角色" prop="roleName" show-overflow-tooltip></el-table-column>
          <el-table-column prop="department" label="科室" show-overflow-tooltip></el-table-column>
          <el-table-column prop="job" label="职称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="level" label="等级" show-overflow-tooltip></el-table-column>
          <el-table-column prop="createDate" label="创建日期" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="keyword.currentPage"
          :page-size="keyword.pageSize"
          class="page"
          small
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </el-main>
    </el-container>
    <EditUser :dialogVisible="dialogVisible" :user="user" :title="title" @show="show" />
  </div>
</template>

<script>
import EditUser from "@/views/user/EditUser.vue";
import { getUsers, remove, getUser } from "@/api/user.js";
import { getRoles } from "@/api/role.js";
import { getDepartments, getLevels } from "@/api/common.js";

export default {
  name: "",
  components: {
    EditUser,
  },
  data() {
    return {
      title: "添加用户",
      keyword: { currentPage: 1, pageSize: 10 },
      total: 0,
      dialogVisible: false,
      userData: [],
      departments: [],
      roleData: [],
      department: "",
      levels: [],
      level: "",
      user: {job:{code:""},department:{code:""},level:{code:""},role:{id:""}},
    };
  },
  created() {
    this.loadUsers(false);
    // 加载科室
    this.loadDepartment();
    //
    this.loadLevels();
    //
    this.loadRoles();
  },
  methods: {
    loadRoles() {
      getRoles().then((res) => {
        if (res.data.code === "0000") {
          this.roleData = res.data.data;
        }
      });
    },
    loadUsers(val) {
      if (val) {
        this.keyword.currentPage = 1;
      }
      getUsers(this.keyword).then((res) => {
        if (res.data.code === "0000") {
          this.userData = res.data.data.records;
          this.total = Number.parseInt(res.data.data.total);
          console.log(this.total);
        }
      });
    },
    loadDepartment() {
      getDepartments().then((res) => {
        if (res.data.code === "0000") {
          this.departments = res.data.data;
        }
      });
    },
    loadLevels() {
      getLevels().then((res) => {
        if (res.data.code === "0000") {
          this.levels = res.data.data;
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleEdit(index, row) {
      this.title = "编辑用户";
      this.dialogVisible = true;
      getUser(row.id).then((res) => {
        if (res.data.code === "0000") {
          this.user = res.data.data;
        }
      });
      console.log(index, row);
    },
    handleDelete(index, row) {
      this.$confirm("确认要删除该用户吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log(row.id);
          remove(row.id).then((res) => {
            if (res.data.code == "0000") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.loadUsers();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addUser() {
      this.title = "添加用户";
      this.dialogVisible = true;
    },
    show() {
      this.dialogVisible = false;
      this.loadUsers(false);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.keyword.currentPage = val;
      this.loadUsers(false);
      console.log(`当前页: ${val}`);
    },
  },
};
</script>

<style lang="scss" scoped>
.keyword {
  text-align: left;
}
.page {
  margin-top: 10px;
}
</style>